package com.woniuxy.yoga.commons.xxl.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.woniuxy.yoga.commons.xxl.config.XxlJobConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Component
public class XxlUtil {

    @Autowired
    private XxlJobConfig xxlJobConfig;

    public int Login(){
        String xxlLoginAPI=xxlJobConfig.getAdminAddresses()+"/login";
        HashMap<String, Object> loginParam = new HashMap<>();
        loginParam.put("userName", "admin");
        loginParam.put("password", "123456");
        HttpResponse httpResponse = HttpRequest.post(xxlLoginAPI).form(loginParam).execute();
        return httpResponse.getStatus();
    }
    /**
     * 添加定时任务
     * @param time 执行时间
     * @param tackName 任务名称
     * @param author 作者
     * @param JobHeader XxlJob处理路径
     * @param param 参数
     * @return 响应体body的JOSN字符串
     */
    public String addTack(LocalDateTime time,String tackName,String author,String JobHeader,String param){
        //登录
        String body = null;
        if(Login()==200) {
            System.out.println("xxl登录成功");
            Map<String, Object> addTaskParam = new HashMap<>();
            StringBuilder cron = new StringBuilder();
            String crons = time.getSecond() + " " + time.getMinute() + " " + time.getHour() + " " + time.getDayOfMonth() + " " + time.getMonth().getValue() + " ? " + time.getYear() + "-" + time.getYear();
            cron.append(crons);
            System.out.println(cron.toString());
            //设置请求参数
            addTaskParam.put("jobGroup", 2);//执行器
            addTaskParam.put("jobDesc", tackName);//任务名称
            addTaskParam.put("author", author);//作者
            //addTaskParam.put("alarmEmail","@xxx.com");//邮箱
            addTaskParam.put("scheduleType", "CRON");//调度类型
            addTaskParam.put("scheduleConf", cron.toString());//Cron表达式
            addTaskParam.put("glueType", "BEAN");//运行模式
            addTaskParam.put("executorHandler", JobHeader);//JobHandler
            addTaskParam.put("executorParam",param);//参数
            addTaskParam.put("executorRouteStrategy", "FIRST");//路由策略
            addTaskParam.put("misfireStrategy","DO_NOTHING");//调度过期策略
            addTaskParam.put("executorBlockStrategy", "SERIAL_EXECUTION");//阻塞处理策略
            addTaskParam.put("executorTimeout", "0");//任务超时时间
            addTaskParam.put("executorFailRetryCount","0");//失败重试次数
            addTaskParam.put("triggerStatus", 1);
            //发送请求
            String xxlAddJobAPI = xxlJobConfig.getAdminAddresses()+"/jobinfo/add";
            HttpResponse response = HttpRequest.post(xxlAddJobAPI).form(addTaskParam).execute();
            if(response.getStatus()!=200) {
                throw new RuntimeException("自定义定时任务失败");
            }
            System.out.println("定时任务设置成功");
            body = response.body();
        }else{
            System.out.println("xxl登录失败");
        }
        return body;
    }

    /**
     * 删除定时任务
     * @param id 任务ID
     */
    public void removeTack(Long id){
        //登录
        if (Login()==200){
            String xxlRemoveJobAPI = xxlJobConfig.getAdminAddresses()+"/jobinfo/remove";
            HashMap<String, Object> param = new HashMap<>();
            param.put("id",id);
            HttpResponse response = HttpRequest.post(xxlRemoveJobAPI).form(param).execute();
            if (response.getStatus()==200){
                System.out.println(id+"号任务，删除成功！");
            }else{
                System.out.println(id+"号任务，删除失败！");
            }
        }else{
            System.out.println("xxl登录失败");
        }
    }

}
